var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.parent.frames["zwIframe"];
if (!upWindow) {
    upWindow = window.parent.parent.frames["componentDiv"].frames["zwIframe"];
}
function fy_dialog_close() {
//	window.parent.parent.$(".mask").remove();
//	window.parent.parent.$(".layui-layer-iframe").parent().remove();
    window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");

}

function getUpFields(name) {
    return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}
function getUpAccountFieldValue(name) {
    var field = getUpFields(name).eq(0);
    return {
        displayValue: field.is("span") ? field.text() : field.val(),
        value: eval("(" + field.parent("span").attr("fieldVal") + ")").value
    };
}
// 添加或者删除行
function addOrDelRepeat(name, newLength) {
    var fields = getUpFieldsByFieldval(name);
    var oldLength = fields.length;
    var tbody = getUpFieldsByFieldval(name).eq(0).parent();
    var count = 0;
    while (!tbody.is("tbody") && count < 15) {
        tbody = tbody.parent();
        count++;
    }
    if (newLength != oldLength)
        for (var i = 0, length = Math.abs(newLength - oldLength); i < length; i++) {
            if (newLength > oldLength) {
                upWindow.fy_addEmpty(tbody.children().last());
            } else {
                upWindow.fy_delCurrentRepeat(tbody.children().last());
            }
        }
}
function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":

            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "checkbox":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "radio":
            if (editTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            }
            break;
        case "select":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + " option:selected")
                        .attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).attr("val4cal");
            }
            break;
        case "date":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
                if (obj.value != "") {
                    obj.value = obj.value + " 00:00";
                }
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "datetime":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "flowdealoption":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "lable":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relationform":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relation":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "project":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + "_txt").val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).text();
            }
            break;
        case "attachment":
        case "image":
        case "document":
            if (editTag || browseTag) {
                var attNames = "";
                var tempField = field.find("#" + fieldVal.name);
                var tempFileId = "";
                if (tempField[0].tagName.toLowerCase() == "input") {
                    tempFileId = tempField.val();
                } else if (tempField[0].tagName.toLowerCase() == "span") {
                    tempFileId = tempField[0].innerHTML;
                }
                var atts = upWindow.getAttBySubreference(tempFileId);
                for (var j = 0; j < atts.length; j++) {
                    attNames += atts[j].filename + ",";
                }
                if (attNames != "") {
                    attNames = attNames.substr(0, attNames.length - 1);
                }
                obj.value = attNames;
            }
            break;
        case "outwrite":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "externalwrite-ahead":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "exchangetask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "querytask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}
function setUpFieldValue(name, value) {
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input").val(
            value);
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
            .trigger("blur");
}

function setUpFieldValue2(field, value, check) {

    try {

        var fieldVal = field.attr("fieldVal");
        var editTag = field.hasClass(editClass);
        var addTag = field.hasClass(addClass);
        value = trim(value);
        // displayValue = trim(displayValue);
        if (fieldVal != null && typeof (fieldVal) != 'undefined') {
            fieldVal = eval("(" + fieldVal + ")");
        } else {
            return null;
        }

        var tag = true;
        switch (fieldVal.inputType) {
            case "text":
            case "textarea":
                if (editTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    // inField.attr("readonly", true);
                    if (check)
                        inField.trigger("blur");
                } else if (addTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    // inField.attr("readonly", true);
                    if (check)
                        inField.trigger("blur");
                }
                break;
            case "checkbox":
                if (editTag) {
                    field.find("#" + fieldVal.name).attr("checked", true);
                    if (check)
                        field.find("#" + fieldVal.name).trigger("click");
                }
                break;
            case "radio":
                if (editTag) {
                    field.find(":radio").eq(index).attr("checked", true);
                    if (check)
                        field.find(":radio").eq(index).trigger("click");
                }
                break;
            case "select":
                if (editTag) {
                    // if (value) {
                    // field.find("#" + fieldVal.name).value(value);
                    // } else {
                    var options = field.find("#" + fieldVal.name + " option");
                    for (var k = 0; k < options.length; k++) {
                        if (options.eq(k).text() == value)
                            options.eq(k).attr("selected", true);
                    }
                    // }
                    field.find("#" + fieldVal.name).trigger("change");
                }
                break;
            case "date":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    if (check)
                        field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "datetime":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "member":
            case "multimember":
            case "account":
            case "multiaccount":
            case "department":
            case "multidepartment":
            case "post":
            case "multipost":
            case "level":
            case "multilevel":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                }
                break;
        }
    } catch (e) {
        // TODO: handle exception
    }
}

/**
 * 去除左右空格
 */
function trim(str) {
    return (str) ? (typeof str == "string" ? str.replace(/^\s*/, "").replace(
            /\s*$/, "") : str) : "";
}

function getParentTR(dom) {
    if (dom) {
        if (dom.tagName.toLowerCase() == 'tr') {
            return dom;
        } else {
            return getParentTR(dom.parentNode);
        }
    } else {
        return null;
    }
}

function indexOf_Array(array, fieldName) {
    if (array) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] == fieldName) {
                return i;
            }
        }
    }
    return -1;
}
function writeToForm() {
    var length = data.length;
    var fieldNameArray = getUpFieldsByFieldval("导入字段列表").length > 0 ? getUpFieldValue(getUpFieldsByFieldval(
            "导入字段列表").eq(0)).value.split(',')
            : (getUpFieldsByFieldval("导入字段列表2").length > 0 ? getUpFieldValue(getUpFieldsByFieldval(
                    "导入字段列表2").eq(0)).value.split(',')
                    : []);
    var dateFieldArray = getUpFieldsByFieldval("日期字段列表").length > 0 ? getUpFieldValue(getUpFieldsByFieldval(
            "日期字段列表").eq(0)).value.split(',')
            : [];
    var fieldNameCount = fieldNameArray.length;
    var firstFieldArray = getUpFieldsByFieldval(fieldNameArray[0]);
    var rowSize = firstFieldArray.length;
    // var tbody = firstFieldArray.eq(0).parent();
    // while (!tbody.is("tbody")) {
    // tbody = tbody.parent();
    // }

    try {
        console.log("导入字段列表 length:" + getUpFieldsByFieldval("导入字段列表").length);
        console.log("导入字段列表2 length:" + getUpFieldsByFieldval("导入字段列表2").length);
        console.log("重复表增加删除2 value:" + getUpFieldValue(getUpFieldsByFieldval(
                "重复表增加删除2").eq(0)).value);
        console.log(getUpFieldsByFieldval("导入字段列表").length > 0 ? true
                : false);
        console.log(getUpFieldsByFieldval("导入字段列表").length > 0 ? true
                : (getUpFieldsByFieldval("导入字段列表2").length > 0 ? getUpFieldValue(getUpFieldsByFieldval(
                        "重复表增加删除2").eq(0)).value
                        : true));
    } catch (e) {
    }

    if (getUpFieldsByFieldval("导入字段列表").length > 0) {
        if ((getUpFieldsByFieldval("导入字段列表2").length > 0 ? getUpFieldValue(getUpFieldsByFieldval(
                "重复表增加删除2").eq(0)).value
                : true))
            addOrDelRepeat(fieldNameArray[0], length);
        // for ( var i = 0; i < length; i++) {
        // trNode.id = "oper";
        // window.dialogArguments.addEmpty();
        // }
        //
        // for ( var i = 0; i < rowSize; i++) {
        // getParentTR($2("" + fieldNameArray[0])[0]).id = "oper";
        // window.dialogArguments.del();
        // }
    }

    for (var j = 0; j < fieldNameCount; j++) {
        try {
            var dateFieldIndex = indexOf_Array(dateFieldArray,
                    fieldNameArray[j]);
            for (var i = 0; i < length; i++) {
                var fieldValue = (data[i][j] ? (data[i][j] == "undefined" ? ""
                        : data[i][j]) : "");
                if (dateFieldIndex != -1) {
                    if (fieldValue.indexOf("/") != -1) {
                        var tempArray = fieldValue.split("/");

                        if (tempArray.length == 3) {
                            var temp = [];
                            temp.push(tempArray[0]);
                            temp.push(tempArray[1].length == 1 ? "-0" : "-");
                            temp.push(tempArray[1]);
                            temp.push(tempArray[2].length == 1 ? "-0" : "-");
                            temp.push(tempArray[2]);

                            fieldValue = temp.join("");
                        }
                    }
                }
                // if ($2("" + fieldNameArray[j])[i].tagName.toUpperCase() ==
                // "SELECT") {
                //
                // var options = $2("" + fieldNameArray[j])[i].options;
                // for ( var k = 0; k < options.length; k++) {
                // if (options[k] && options[k].text == fieldValue) {
                // options[k].selected = true;
                // break;
                // }
                // }
                //
                // } else {
                setUpFieldValue2(
                        getUpFieldsByFieldval(fieldNameArray[j]).eq(i),
                        fieldValue, (i == (length - 1)));
                // }
                // if (i == (length - 1)) {
                // window.dialogArguments.calc($2("" + fieldNameArray[j])[i]);
                // }
            }
        } catch (e) {
        }
    }
    fy_dialog_close();
}
